package com.guigu.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.guigu.health.common.UploadFiles;
import com.guigu.health.pojo.*;
import com.guigu.health.service.SetmealService;
import com.mysql.jdbc.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @ClassName:SetMealController
 * @Description:套餐管理控制层
 * @Author:longtao
 * @Date:2021/4/12
 * @Version:1.0
 */
@RequestMapping("/setMeal")
@Controller
public class SetMealController extends BaseController {
    @Reference
    private SetmealService setmealService;


    @RequiresPermissions({"SETMEALPIECHART"})
    @ResponseBody
    @RequestMapping(value = "/packageNumberPieChart", method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    public Object packageNumberPieChart() {
        /**
         * 套餐数量
         */
        List<Map<String, Object>> maps = setmealService.numberOfBookingPackages();
        /**
         * 套餐名称集合
         */
        List<String> list = new ArrayList<String>();

        List<Long> list2 = new ArrayList<Long>();
        /**
         * 抽取套餐名称
         */
        if (null != maps) {
            for (Map<String, Object> map : maps) {
                /**
                 * 获取套餐名称
                 */
                list.add((String) map.get("name"));
                list2.add((Long) map.get("count"));
            }
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        /**
         * 添加返回结果
         */
        resultMap.put("setMealNames", list);
        resultMap.put("setMealCount", maps);
        return resultMap;
    }

    /**
     * 显示套餐
     *
     * @param name
     * @param pageNum
     * @return
     */
    @RequiresPermissions({"SETMEAL_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showSetMeal")
    public Object showSetMeal(String name, String pageNum) {
        if (StringUtils.isNullOrEmpty(pageNum)) {
            pageNum = "1";
        }
        PageInfo<Setmeal> pageInfo = null;
        if (StringUtils.isNullOrEmpty(name)) {
            pageInfo = setmealService.queryAllByName(null, Integer.valueOf(pageNum), 2);
        } else {
            SetmealExample example = new SetmealExample();
            SetmealExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%" + name + "%");
            pageInfo = setmealService.queryAllByName(example, Integer.valueOf(pageNum), 2);
        }
        return pageInfo;
    }

    /**
     * 根据id删除套餐
     *
     * @param id
     * @return
     */
    @RequiresPermissions({"SETMEAL_DELETE"})
    @ResponseBody
    @RequestMapping(value = "/deleteSetMeal")
    public Object deleteSetMeal(String id) {
        return setmealService.deleteSetMeal(Integer.valueOf(id));
    }

    /***
     * 套餐修改
     * @param setmeal
     * @return
     */
    @RequiresPermissions({"SETMEAL_EDIT"})
    @ResponseBody
    @RequestMapping(value = "/updateSetMeal", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    public Object updateSetMeal(@RequestBody Setmeal setmeal,int [] checkGroupIds) {
        SetmealExample example = new SetmealExample();
        SetmealExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(setmeal.getId());
        boolean b = setmealService.updateSetMeal(setmeal, example,checkGroupIds);
        return b;
    }

    /**
     * 套餐文件上传
     *
     * @param multipartFile
     * @param id
     * @return
     */
    @RequiresPermissions({"SETMEAL_EDIT", "SETMEAL_ADD"})
    @ResponseBody
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public Object uploadImg(@RequestParam("file") MultipartFile multipartFile, String id) {
        boolean b = false;
        /**
         * 获取到原有图片名称,截图到后缀名
         */
        String originalFilename = multipartFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String a = multipartFile.getContentType();
        /**
         * 生成唯一文件名,拼接后缀名
         */
        String fileName = UUID.randomUUID() + extension;
        try {
            UploadFiles.uploadViaByte(multipartFile.getBytes(), fileName);
            Map<String, String> map = new HashMap<String, String>();
            map.put("imgName", fileName);
            map.put("domain", UploadFiles.DOMAIN);
            /**
             * 输出url路径
             */
            String path = UploadFiles.DOMAIN + fileName;
            Setmeal setmeal = new Setmeal();
            setmeal.setImg(path);
            SetmealExample example = new SetmealExample();
            SetmealExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(Integer.valueOf(id));
            b = setmealService.updateSetMeal(setmeal, example,null);
        } catch (IOException e) {
            e.printStackTrace();
            b = false;
        }
        return b;
    }


    /**
     * 显示检查组中的检查项,用于添加
     *
     * @return
     */
    @RequiresPermissions({"SETMEAL_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showInspectionGroupInspectionItems", method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    public Object showInspectionGroupInspectionItems() {
        List<CheckGroup_CheckItem> items = setmealService.queryAllCheckGroupCheckItem();
        return items;
    }

    /**
     * 新增套餐
     * @param map
     * @return
     */
    @RequiresPermissions({"SETMEAL_ADD"})
    @ResponseBody
    @RequestMapping(value = "/addSetMeal", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    public Object addSetMeal(@RequestBody Map<Object, Object> map) {
        boolean flag = false;
        /**
         * setmeal对象字符串
         */
        String o = String.valueOf(map.get("setmeal"));
        /**
         * checkGroupKey集合字符串
         */
        String key = String.valueOf(map.get("checkgroupKey"));
        /**
         * 转为SetMeal对象
         */
        Setmeal parse = JSONObject.parseObject(o, Setmeal.class);
        /**
         * 先新增套餐,然后在添加别的
         */
        flag = setmealService.addSetMeal(parse);
        /**
         * 转为list集合
         */
        List<String> list = JSONObject.parseArray(key, String.class);
        List<Setmeal> list1 = setmealService.selectByExample(null);
        Integer id = list1.get(list1.size() - 1).getId();
        List<Setmeal_CheckgroupKey> checkItemList = new ArrayList<Setmeal_CheckgroupKey>();
        for (String s : list) {
            if (!StringUtils.isNullOrEmpty(s)) {
                Setmeal_CheckgroupKey checkGroup_checkItem = new Setmeal_CheckgroupKey();
                checkGroup_checkItem.setSetmeal_id(id);
                checkGroup_checkItem.setCheckgroup_id(Integer.valueOf(s));
                checkItemList.add(checkGroup_checkItem);
            }
        }

        flag = setmealService.addSetMealCheckGroup(checkItemList);
        return flag;
    }

    @RequiresPermissions(value = "SETMEAL_QUERY")
    @ResponseBody
    @RequestMapping(value = {"/showCheckGroup"},method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object showCheckGroup(){
        List<CheckGroup> list = setmealService.queryAllCheckGroup();
        return list;
    }

    @RequiresPermissions(value = {"SETMEAL_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showCheckGroupById",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object showCheckGroupById(String id){
        List<Integer> list = setmealService.getCountCheckGroupIds(Integer.valueOf(id));
        return list;
    }

    /**
     * 判断编码是否一致
     * @param count
     * @return
     */
    public Object count(String count){
        int i = setmealService.count(count);
        boolean flag=true;
        if (i>0){
            flag=false;
        }
        return flag;
    }
}
